﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Weight.Base
{
    [AttributeUsage(AttributeTargets.Property,AllowMultiple =true)]
   public abstract  class BaseAttribute:Attribute
    {
        public virtual String error { get; set; }

        public abstract bool Validate(object val);
    }

    /// <summary>
    /// 必填项不能为空
    /// </summary>
    public class RequiredAttribute : BaseAttribute
    {
        public override string error { get {
                if (base.error != null)
                {
                    return base.error;
                }
                return "属性不能为空";
            }

            set => base.error = value; }
        public override bool Validate(object val)
        {
            string value = Convert.ToString(val).Trim();
            if(val == null || value.Length == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
